<?php
namespace GRH\Model\Table;

use Zend\Db\Adapter\Adapter;
use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\ResultSet\ResultSet;

use Zend\Db\Sql\Sql;
use Zend\Db\Sql\Select;
use Zend\Db\Sql\Where;
use Zend\Db\Sql\Expression;

use GRH\Model\Planning;


class PlanningTable extends AbstractTableGateway
{
    protected $table = 'grh_planning';
    
    /**
     * Constructeur
     * 
     * @param \Zend\Db\Adapter\Adapter $adapter
     */
    public function __construct(Adapter $adapter)
    {
        $this->adapter = $adapter;
        $this->resultSetPrototype = new ResultSet();
        $this->resultSetPrototype->setArrayObjectPrototype(new Planning());
        $this->initialize();
    }
    
    /**
     * Tous les plannings
     * 
     * @return Collection
     */
    public function getAll()
    {
        return $this->select();
    }
    
    /**
     * Tous les plannings d'une date précise
     * 
     * @param type $date
     * @return type
     */
    public function getAllByDate($date)
    {
        return $this->select(array('journee' => $date));
    }
    
    /**
     * Planning durant une période
     * 
     * @param type $begin
     * @param type $end
     * @return type
     */
    public function getAllByDatePeriode($begin, $end)
    {
        return $this->select(function(Select $select){
            $where = new Where();
            $where->between('journee', $begin, $end);
            $select->where($where);
        });
    }
    
    /**
     * Récupérer un planning
     * 
     * @param type $id
     * @return type
     * @throws \Exception
     */
    public function get($id)
    {
        $id = (int)$id;
        $row = $this->select(array('planning_id' => $id))
                ->current();
        if(!$row)
            throw new \Exception("PlanningTable::get => Impossible de trouver '$id'");
        
        return $row;
    }
    
    /**
     * Sauvegarder une planning
     * 
     * @param \GRH\Model\Planning $planning
     * @throws \Exception
     */
    public function save(Planning $planning)
    {
        $data = array(
            'employe_id' => $planning->employe_id,
            'periode_id' => $planning->periode_id,
            'journee' => $planning->journee,
            'heure_entree' => $planning->heure_entree,
            'heure_sortie' => $planning->heure_sortie,
            'nb_heure' => $planning->nb_heure,
            'code_activite' => $planning->code_activite_id,
            'details_activite' => $planning->details_activite,
            'repas' => $planning->repas,
            'pause' => $planning->pause,
            'type_planning' => $planning->type_planning,
        );
        $id = (int)$id;
        if($id == 0)
            $this->insert ($data);
        else
            if($this->get($id))
                $this->update($data, array('planning_id' => $id));
            else
                throw new \Exception("Impossible de trouver '$id'");
            
        if($id == 0) return $this->lastInsertValue;
        else return $id;
    }
    
    /**
     * Ajouter un planning
     * 
     * @param \GRH\Model\Planning $planning
     */
    public function add(Planning $planning)
    {
        $data = array(
            'employe_id' => $planning->employe_id,
            'periode_id' => (int)$planning->periode_id,
            'journee' => $planning->journee,
            'heure_entree' => $planning->heure_entree,
            'heure_sortie' => $planning->heure_sortie,
            'nb_heure' => (int)$planning->nb_heure,
            'code_activite' => $planning->code_activite_id,
            'details_activite' => is_null($planning->details_activite) ? '' : $planning->details_activite,
            'repas' => $planning->repas,
            'pause' => $planning->pause,
            'type_planning' => (int)$planning->type_planning,
        );
        $this->insert($data);
        return $this->lastInsertValue;
    }
    
    /**
     * Effacer un planning
     * 
     * @param type $id
     */
    public function delete($id)
    {
        $this->delete(array('planning_id' => $id));
    }
}